import datetime
from typing import Iterable

from PyQt5.QtWidgets import QWidget

from dao.noteDao import NoteDao
from dao.typeDao import TypeDao
from domain.note.noteDomain import NoteDomain
from domain.util.pageDomain import PageDomain
from domain.util.tableColumn import TableColumn
from model.components.tableWidget import TableWidget, decorateButton
from model.note.modules.addNote import AddNote
from util.aop.ormProxy import OrmProxy
from util.qt import qtUtil
from util.singleton import Singleton
from view.note.ui_noteWidget import Ui_NoteWidget


import functools
import sys

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget, QPushButton, QHBoxLayout, QMessageBox


@Singleton
class NoteWidget(QWidget, Ui_NoteWidget):

    def listNotes(self, param: PageDomain):
        OrmProxy.startPage(param.pageNum, param.pageSize, 'note_id desc')
        result = self.noteDao.list4Table(**self.queryParam)
        return OrmProxy.getTotal(result), result

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.btnAdd.clicked.connect(lambda: self.add)
        self.noteDao = NoteDao.getSingleton()
        self.typeDao = TypeDao.getSingleton()
        self.addNote = None
        self.editNote = None

        self.typeList = []
        self.queryParam = {
            'noteType': None,
            'noteContent': None,
            'dateRange': None
        }
        self._columns = [
            TableColumn('日期', 'noteDate', textFilter=lambda x, _:str(x), width=150),
            TableColumn('开始', 'startTime', textFilter=lambda x, _:str(x), width=100),
            TableColumn('结束', 'endTime', textFilter=lambda x, _:str(x), width=100),
            TableColumn('类型', 'noteType', textFilter=self.typeFilter, width=150),
            TableColumn('内容', 'noteContent', textFilter=lambda x, _:x if len(x) <= 36 else x[:33] + '...'),
            TableColumn('操作', 'action', self.actionButtons, width=0),
        ]
        self.table = TableWidget(self._columns, self.listNotes)
        qtUtil.nestWidget(self.frame, self.table)

        self.cmbType.currentIndexChanged.connect(lambda x: self.queryParam.update({'noteType': x}))
        self.editContent.textChanged.connect(lambda x: self.queryParam.update({'noteContent': x}))
        self.btnQuery.clicked.connect(lambda: self.table.refresh())
        self.btnAdd.clicked.connect(lambda: self.add())
        self.init()

    def init(self):
        self.typeList = self.typeDao.list()
        self.cmbType.clear()
        self.cmbType.addItem(None, None)
        for row in self.typeList:
            self.cmbType.addItem(row.typeName, row.typeId)
        self.table.refresh()

    def add(self):
        self.addNote = AddNote()
        self.addNote.show()

    def edit(self, row):
        pass

    def delete(self, noteId):
        pass

    def typeFilter(self, typeId, _):
        for row in self.typeList:
            if row.typeId == typeId:
                return row.typeName
        return ''

    def changeTransparency(self, i):
        op = QtWidgets.QGraphicsOpacityEffect()
        op.setOpacity(i)
        self.setGraphicsEffect(op)

    def actionButtons(self, _, row) -> Iterable[QWidget]:
        btnEdit = QPushButton('编辑')
        btnEdit.clicked.connect(functools.partial(self.edit, row))
        btnRemove = QPushButton('删除')
        btnRemove.clicked.connect(functools.partial(self.delete, row.noteId))
        return decorateButton(btnEdit), decorateButton(btnRemove, 'red')


if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = NoteWidget()
    widget.show()
    app.exec_()

